package com.wedimob.html;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Form {
    private String origin;
	private String id;
	private String method;
	private String action;
	private List<FormField> fields = new ArrayList<FormField>();

	public Form(String origin, String id, String method, String action) {
		super();
		this.origin = origin;
		this.id = id;
		this.method = method;
		this.action = action;
	}

    public String origin() {
        return origin;
    }
    
	public String getId() {
		return id;
	}

	public String getMethod() {
		return method;
	}

	public String getAction() {
		return action;
	}

	public Iterator<FormField> fields() {
		return fields.iterator();
	}

	public void clearFields() {
		fields.clear();
	}

	public void addField(FormField field) {
		fields.add(field);
	}
	
	public FormField getField(String name) {
		for (FormField field : fields) {
			if (name.equals(field.getName())) {
				return field;
			}
		}
		throw new RuntimeException("Cannot find field " + name);
	}
	
	@Override
	public String toString() {
		StringBuilder bd = new StringBuilder();
		bd.append("id=");
        bd.append(id);
		bd.append(", action=");
		bd.append(action);
		bd.append(", method=");
		bd.append(method);
		bd.append(", Fields[");
		int i = 0;
		for(FormField field : fields) {
			if(i>0)
				bd.append(", ");
			bd.append(field.getName());
			bd.append("=");
			bd.append(field.getValue());
			i++;
		}
		bd.append("]");
		return bd.toString();
	}
}
